package org.opsli.plugins.Email;

import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.opsli.common.utils.CheckStrength;
import org.opsli.core.options.EmailConfigFactory;
import org.opsli.core.utils.GuaGuaUtil;
import org.opsli.plugins.email.EmailPlugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 邮箱重置密码
 */
@Slf4j
@Component
public class EmailResetPassword {


    @Autowired
    private EmailPlugin emailPlugin;

    /**
     * 发送重置邮件
     */
    public boolean sendResetEmail(String email) {
        if (CheckStrength.StringUtils.equalsNull(email)) {
            return false;
        }
        // 获取code
        String token = generateToken(email);
        String url = "http://localhost:5173/resetPassword?token=" + token;
        String html = "<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                "    <title>Password Reset Request</title>\n" +
                "    <style>\n" +
                "        body {\n" +
                "            font-family: 'Helvetica Neue', Arial, sans-serif;\n" +
                "            line-height: 1.6;\n" +
                "            color: #333333;\n" +
                "            max-width: 600px;\n" +
                "            margin: 0 auto;\n" +
                "            padding: 20px;\n" +
                "        }\n" +
                "        .header {\n" +
                "            text-align: center;\n" +
                "            padding: 20px 0;\n" +
                "        }\n" +
                "        .logo {\n" +
                "            max-width: 150px;\n" +
                "        }\n" +
                "        .content {\n" +
                "            background-color: #f9f9f9;\n" +
                "            padding: 30px;\n" +
                "            border-radius: 8px;\n" +
                "        }\n" +
                "        .button {\n" +
                "            display: inline-block;\n" +
                "            padding: 12px 24px;\n" +
                "            background-color: #2563eb;\n" +
                "            color: white !important;\n" +
                "            text-decoration: none;\n" +
                "            border-radius: 4px;\n" +
                "            font-weight: bold;\n" +
                "            margin: 20px 0;\n" +
                "        }\n" +
                "        .footer {\n" +
                "            text-align: center;\n" +
                "            margin-top: 30px;\n" +
                "            color: #777777;\n" +
                "            font-size: 12px;\n" +
                "        }\n" +
                "        .code {\n" +
                "            font-family: monospace;\n" +
                "            font-size: 18px;\n" +
                "            letter-spacing: 2px;\n" +
                "            background-color: #f0f0f0;\n" +
                "            padding: 8px 12px;\n" +
                "            border-radius: 4px;\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <div class=\"header\">\n" +
                "        <!-- Replace with your logo -->\n" +
                "        <img src=\"https://example.com/logo.png\" alt=\"Company Logo\" class=\"logo\">\n" +
                "    </div>\n" +
                "    \n" +
                "    <div class=\"content\">\n" +
                "        <h2>Password Reset Request</h2>\n" +
                "        \n" +
                "        <p>Hello [" + email + "],</p>\n" +
                "        \n" +
                "        <p>We received a request to reset your password for your [Hai Money] account.</p>\n" +
                "        \n" +
                "        <p>To reset your password, please click the button below:</p>\n" +
                "        \n" +
                "        <div style=\"text-align: center;\">\n" +
                "            <!-- Replace YOUR_RESET_LINK with your actual reset link -->\n" +
                "            <a href=\"" + url + "\" class=\"button\">\n" +
                "                Reset Password\n" +
                "            </a>\n" +
                "        </div>\n" +
                "        \n" +
                "        <p>This link will expire in <strong>1 hours</strong> for security reasons.</p>\n" +
                "        \n" +
                "        <p>If you didn't request this password reset, please ignore this email or contact our support team immediately.</p>\n" +
                "        \n" +
                "        <p>Best regards,<br>\n" +
                "        The [Hai Money] Team</p>\n" +
                "    </div>\n" +
                "    \n" +

                "</body>\n" +
                "</html>";

        //发送验证码
        try {
            String msg = emailPlugin
                    .send(email, "Hai Money Reset Password", html, true,
                            EmailConfigFactory.INSTANCE.getConfig());
            log.info("发送验证码:{}", msg);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 校验重置邮件
     */
    public boolean checkCode(String email, String code) {
        String key = "email:Reset-" + email;
        Object cache = GuaGuaUtil.get(key);
        if (cache == null) {
            return false;
        }
        return code.equals(cache.toString());
    }


    /**
     * 生成重置邮件并存储
     */
    public String generateToken(String email) {
        //生成重置邮件
        String token = RandomUtil.randomString(10);
        //存储重置邮件
        GuaGuaUtil.set("email:Reset-" + email, token, 60 * 60, TimeUnit.SECONDS);
        return token;
    }


}

